<?php
/**
 * @author wyl
 * @date 2015-8-4 16:29:04
 */
Yii::import('system.web.widgets.CWidget');
class CDialog extends CWidget{
    
    public $title;
    public $buttons;
    
    public $modalClass = '';
    
    public $htmlOptions = array();
    public $width;
    public $height;
    
	public function init(){
	   $this->htmlOptions['id'] = $this->getId();
	   $this->htmlOptions['class'] = 'modal fade '.$this->modalClass;
	   $stlye = array();
	   $bodyStyle = array();
	   if ($this->width !== null){
	       $stlye['width'] = 'width:'.$this->width;
	       if (is_numeric($this->width)){
	           $stlye['width'] .='px';
	       }
	   }
	   if ($this->height !== null){

	       $bodyStyle['height'] = 'height:'.$this->height;
	       if (is_numeric($this->height)){
	           $bodyStyle['height'] .='px';
	       }
	   }
	   
	   echo CHtml::openTag('div',$this->htmlOptions);
	       echo CHtml::openTag('div',array('class' => 'modal-dialog','style' => implode(';', $stlye)));
	           echo CHtml::openTag('div',array('class' => 'modal-content'));
	               echo $this->renderHeader();
	               echo CHtml::openTag('div',array('class' => 'modal-body','style' => implode(';', $bodyStyle)));
	}
	
	protected function renderHeader(){
	    return CHtml::tag('div',array('class' => 'modal-header modal-header-mine'),
	       CHtml::htmlButton(CHtml::tag('span',array('aria-hidden' =>'true'),'&times;'),array('class' => 'close','data-dismiss' => 'modal','aria-label' => 'Close')).
	       CHtml::tag('h4',array('class' => 'modal-title'),$this->title)
	    );
	}
	
    protected function renderFooter(){
        echo CHtml::openTag('div',array('class' => 'modal-footer'));
        if (!empty($this->buttons)){
            foreach ($this->buttons as $buttonId => $button){
                
                if (!array_key_exists('id', $button)){
                    $button['id'] = $buttonId;
                }
                
                if (!array_key_exists('title', $button)){
                    $button['title'] = $buttonId;
                }
                
                if (!array_key_exists('class', $button)){
                    $button['class'] = 'btn btn-default ';
                }
                
                if (!array_key_exists('type', $button)){
                    $button['type'] = 'button';
                }
                
                switch ($button['type']){
                    case 'close':
                        $button['htmlOptions']['data-dismiss'] = 'modal';
                        $button['title'] = 'Close';
                        break;
                    case 'button':
                        
                        break;
                }
                
                $this->widget('application.components.widgets.CButton',$button);
            }
        }
        echo CHtml::closeTag('div');
        return null;
	}
	
	public function run(){
	               echo CHtml::closeTag('div');
	               echo $this->renderFooter();
	           echo CHtml::closeTag('div');
	       echo CHtml::closeTag('div');
	    echo CHtml::closeTag('div');
	    $this->buildClient();
	}
	
	protected function buildClient(){
	    Yii::app()->clientScript->registerCss('CDialog',"
			.btn.btn-choose{
				background-color:#1E2225;
				color:#fff;
				border-color:#1E2225;
				padding:6px 25px 6px 25px;
				margin-top:15px;
			}
			.form-horizontal{
				margin-top:30px;
			}
			.modal-header.modal-header-mine{
				background-color:#1E2225;
				color:#fff;
			}
			.modal-header.modal-header-mine span{
				color:#fff;
			}
		");
	    Yii::app()->clientScript->registerScript('CDialog-'.$this->getId(),"
	      	$('#".$this->getId()."').on('shown.bs.modal',function(e){
			   var wHeight	= $(window).height();
	  		   var dHeight	= $(this).find('.modal-dialog').height();
	  		   var top 	= (wHeight/2 - dHeight/2)+'px';
	           $(this).find('.modal-dialog').attr('old-style',$(this).find('.modal-dialog').attr('style'));
	           $(this).find('.modal-dialog').css({
	  	  			'-webkit-transition':'-webkit-transform .4s ease-out',
	  			    '-o-transition'		:'-o-transform .4s ease-out',
	  		    	'transition'		:'transform .4s ease-out',
					'-webkit-transform'	:'translate(0,'+top+')',
					'-ms-transform'		:'translate(0,'+top+')',
					'-o-transform'		:'translate(0,'+top+')',
					'transform'			:'translate(0,'+top+')'
	  	  		});
	        
			});
	      	$('#".$this->getId()."').on('hidden.bs.modal',function(e){
	      	   $(this).find('.modal-dialog').removeAttr('style');
	           $(this).find('.modal-dialog').attr('style',$(this).find('.modal-dialog').attr('old-style'));
			});
		");
	}
	
}